Sliding Window Median
可以用two heap,也可以用multiset
Sliding Window Maximum
可以用deque
次优解是用heap + lazy deletion
LC713 Subarray Product Less Than K
Input: nums = [10, 5, 2, 6], k = 100
Output: 8
Explanation: The 8 subarrays that have product less than 100 are: [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6].
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
LC992 Subarrays with K Different Integers
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
LC340 Longest Substring with At Most K Distinct Characters
Input: s = "eceba", k = 2
Output: 3
Explanation: T is "ece" which its length is 3.
LC992和LC159思路类似,都是维护一个sliding window,然后用一个map保存table里面的内容(key -> element, value -> count)
992稍微复杂一些,需要维护两个sliding window,这两个window右边是对齐的,分别表示从左边到右边有K个数字的最大窗口和最小窗口
LC325 Maximum Size Subarray Sum Equals k
LC152. Maximum Product Subarray
是DP
Input: "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Input: "ABA"
Output: 8
Explanation: The same as example 1, except uni("ABA") = 1.
考虑按照subarray的起点分类
考虑按照每一个元素被几个subarray包含分类(左边界几种可能,右边界几种)
对于每一个字母,边界最远到达字符串结束或者下一个同类字符位置
比如 bdabbabbb,中间的a的左边界最多到第一个a,有3种,右边界到底,最多4种,一共有12个subarray包括了第二个a
Rain Water
Next equal or greater than
LC315 Count of Smaller Numbers After Self(??)
google下雨
安排会议
LC5 Longest Palindromic Substring
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
回文有两种方法
DP P(i,j) = true 如果substring [i,j]是回文的
然后P(i,j) = (P(i+1,j-1) && Si == Sj)
base case: P(i, i) = true P(i, i+1) = Si == Si+1
还有从中间扩展
都是O(N^2)
LC552. Student Attendance Record II
DP即可
可以按照对角线分,也可以投影到两个维度
LC939. Minimum Area Rectangle
直接遍历每一种对角点就行了
LC240. Search a 2D Matrix II
直接可以排除一行或者一列
在完全二叉树的最后一层做binary search:把传统的binary search的寻址方式改一下
数完全二叉树一共有几个节点:先确定深度,然后DFS确定最后一个元素的位置
给一个只有结构的二叉树和inorder array,重新填入值
LC145 Binary Tree Postorder Traversal
summary
对于iterative traversal,一般方法是用一个stack,和一个指针
一个node有两次被访问的时候,第一次是指针指向它,然后压栈的时候,此时子树都没有访问,压栈之后指针指向左子树或者右子树
第二次是弹出的时候(当第一个访问的子树已经到底,开始弹出),此时压栈时候选择的子树已经访问完,指针应该指向另一个子树
对于preorder,在压栈的时候visit,然后指针指向左子树
弹出的时候指针指向右子树
对于inorder,压的时候不访问,然后指针指向左子树
弹出的时候visit,然后指针指向右子树
对于树的访问,不要像递归一样考虑单个子树(先左边完成,然后中间,然后右边)
而是考虑成单个点,这个点一定会被访问,而且是在左右子树都没有访问的时候(preorder)
在左子树访问完的时候(inorder)
对于postorder,访问顺序等于preoder一个左右子树调换的树的反向
所以可以
如果不用这个trick
Morris Traversal
LC1036 Escape a Large Maze
Input 1: a maze represented by a 2D array
0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0
Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (4, 4)
Output: true
Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.
BFS可以,但是DFS不行,因为在此题中,位置是唯一状态,与如何到达该位置无关
DFS+pruning(存position+direction)应该也可以
由此题开始,BFS和DFS的本质区别?
此题就是在一个有向图中搜索某一个点,由于和路径无关,只是找一个点,所以BFS比较合适
在图中DFS,任何一个时刻的状态是一条路径,而BFS,状态是一个波面和一个访问的点
LC490. The Maze
这题DFS+pruning可能也可以,但是normal BFS最方便
Knight's Shortest Path on an Infinite Chessboard
LC127 Word Ladder
LC934 Shortest Bridge
LC433 Minimum Genetic Mutation
External Sort
LC145
LC773
LC315
Segment tree??
还没有归类的
sliding window maximum